package cn.wgx.common.util;

import springfox.documentation.builders.ParameterBuilder;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Parameter;
import springfox.documentation.service.VendorExtension;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.ApiSelectorBuilder;
import springfox.documentation.spring.web.plugins.Docket;

import java.util.ArrayList;
import java.util.List;

public class SwaggerUtil {

    public static String API_TITLE = "API CENTER";

    public static String DESCRIPTION = "这里是<span style='color:red;font-weight:bold;'> ["+API_TITLE+"] </span>模块所有的接口，里边含有说明，仅供测试.返回结果全部为</br>" +
            "{</br>" +
            " 'success': true/false,</br>" +
            " 'data': 结果集 list/entity,</br>" +
            " 'result': '修改结果数量'</br>" +
            " 'msg': '错误信息'</br>" +
            " 'count': '分页查询总数量'</br>" +
            " 'pages': '分页查询总页数'</br>" +
            "}</br>" +
            "如有特殊情况会注明.</br>" +
            "_startDate与_endDate为通用查询字段,所有实体都有,可判断时间范围: " +
            "_startDate < createDate < _endDate";


    protected static ApiInfo apiInfo(String title, String description) {
        ApiInfo apiInfo = new ApiInfo(
                title,
                description,
                "1.0",
                "",
                ApiInfo.DEFAULT_CONTACT,
                "",
                "",
                new ArrayList<VendorExtension>());
        return apiInfo;
    }


    public static Docket createDocket(List<String> basepackage, String title, String description){
        ParameterBuilder tokenPar = new ParameterBuilder();
        tokenPar.name("Authorization").description("令牌头,格式: 'Bearer '+token").
                modelRef(new ModelRef("string")).
                parameterType("header").required(false).build();
        ParameterBuilder tokenPar2 = new ParameterBuilder();
        tokenPar2.name("access_token").description("令牌参数").
                modelRef(new ModelRef("string")).
                parameterType("query").required(false).build();
        List<Parameter> pars = new ArrayList<>();
        pars.add(tokenPar.build());
        pars.add(tokenPar2.build());

        Docket docket = new Docket(DocumentationType.SWAGGER_2);
        docket.apiInfo(apiInfo(title, description ))
                //微服务模式不能分组显示api
                .groupName(Docket.DEFAULT_GROUP_NAME)
                .globalOperationParameters(pars);
        ApiSelectorBuilder select = docket.select();
        for(String pack: basepackage){
            select.apis(RequestHandlerSelectors.basePackage(pack));
        }
        return docket.select().build();
    }


}
